package org.hcf.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.hcf.data.entity.ChatMsg;
import org.hcf.data.mapper.ChatMsgMapper;
import org.hcf.data.service.IChatMsgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 聊天消息 服务实现类
 * </p>
 *
 * @author xianiyitiao
 * @since 2024-04-23
 */
@Service
public class ChatMsgServiceImpl extends ServiceImpl<ChatMsgMapper, ChatMsg> implements IChatMsgService {

    @Override
    public void updateReceive(long msgId, boolean isReceive) {
        update(new LambdaUpdateWrapper<ChatMsg>().eq(ChatMsg::getMsgId, msgId).set(ChatMsg::isReceive, isReceive));
    }

    @Override
    public List<Long> getMessages(long userId, List<Long> groupIds, long msgId) {
        return new ArrayList<>();
//        LambdaQueryWrapper<ChatMsg> query = null;
//        if (groupIds.size() > 0)
//            query = new LambdaQueryWrapper<ChatMsg>().select(ChatMsg::getMsgId).gt(ChatMsg::getMsgId, msgId).and(qw -> qw.and(tqw -> tqw.eq(ChatMsg::getTalkType, ChatType.Friend.getNumber()).and(tqw1 -> tqw1.eq(ChatMsg::getFromId, userId).or().eq(ChatMsg::getToId, userId))).or(tqw -> tqw.eq(ChatMsg::getTalkType, ChatType.Group.getNumber()).in(ChatMsg::getToId, groupIds)));
//        else
//            query = new LambdaQueryWrapper<ChatMsg>().select(ChatMsg::getMsgId).gt(ChatMsg::getMsgId, msgId).and(qw -> qw.and(tqw -> tqw.eq(ChatMsg::getTalkType, ChatType.Friend.getNumber()).and(tqw1 -> tqw1.eq(ChatMsg::getFromId, userId).or().eq(ChatMsg::getToId, userId))));
//        return list(query).stream().map(ChatMsg::getMsgId).collect(Collectors.toList());
    }

    @Override
    public ChatMsg getMessage(long msgId) {
        return getOne(new LambdaQueryWrapper<ChatMsg>().eq(ChatMsg::getMsgId, msgId));
    }

    @Override
    public List<ChatMsg> getMessages(List<Long> msgIds) {
        return list(new LambdaQueryWrapper<ChatMsg>().in(ChatMsg::getMsgId, msgIds));
    }

    @Override
    public ChatMsg getLastMessage(int type, long userId, long to) {
        return null;
//        if (type == ChatType.Friend.getNumber()) {
//            return getOne(new LambdaQueryWrapper<ChatMsg>().ne(ChatMsg::getMsgType, MessageType.Notice_VALUE).eq(ChatMsg::getTalkType, ChatType.Friend.getNumber()).and(qw -> qw.and(tqw2 -> tqw2.eq(ChatMsg::getFromId, userId).eq(ChatMsg::getToId, to)).or(qw2 -> qw2.eq(ChatMsg::getToId, userId).eq(ChatMsg::getFromId, to))).orderByDesc(ChatMsg::getMsgId).last("limit 1"));
//        } else {
//            return getOne(new LambdaQueryWrapper<ChatMsg>().ne(ChatMsg::getMsgType, MessageType.Notice_VALUE).eq(ChatMsg::getTalkType, ChatType.Group.getNumber()).eq(ChatMsg::getToId, to).orderByDesc(ChatMsg::getMsgId).last("limit 1"));
//        }
    }

    @Override
    public long getMaxMsgId() {
        ChatMsg msg = getOne(new LambdaQueryWrapper<ChatMsg>().select(ChatMsg::getMsgId).orderByDesc(ChatMsg::getMsgId).last("limit 1"), false);
        if (msg != null) return msg.getMsgId();
        return 0;
    }
}
